using UnityEngine;
using System.Collections.Generic;

public enum KEYTYPE
{
	CELL,
	MATERIAL,
	BG,
	PARALLAX,
	OBJECTS,
	PREFAB,
	TUTORIAL
}

[RequireComponent(typeof(LevelGrid))]
public class LevelAssetManager:MonoBehaviour
{
	sealed public class CellComponent
	{
		public string name;
		public Texture2D image;
		
		public CellComponent(string name, Texture2D image)
		{
			this.name = name;
			this.image = image;
		}
	}
	
	sealed class DirKeyPair
	{
		public KEYTYPE keyType	{get; set;}
		public string directory {get; set;}
	}
	
	
	readonly DirKeyPair[] dirKeyPairs =
	{
		new DirKeyPair{directory = "CellTextures",      keyType = KEYTYPE.CELL},
		new DirKeyPair{directory = "CellMaterials",     keyType = KEYTYPE.MATERIAL},
		new DirKeyPair{directory = "BGTexture",         keyType = KEYTYPE.BG},
		new DirKeyPair{directory = "ParallaxTextures",  keyType = KEYTYPE.PARALLAX},
		new DirKeyPair{directory = "Objects",  			keyType = KEYTYPE.OBJECTS},
		new DirKeyPair{directory = "PrefabObjects",  	keyType = KEYTYPE.PREFAB},
		new DirKeyPair{directory = "Tutorial",  	    keyType = KEYTYPE.TUTORIAL}
	};
	
	public GameObject cell;
	public GameObject sideBoundary;
	Transform[] boundaries;
	
	Dictionary<KEYTYPE, Dictionary<string, Object>> assetList; 
	
	string currentCell = "A1";
	
	static LevelAssetManager instance;
	const string bgDir = "BGTexture";
	
	void Awake()
	{
		instance = this;
		assetList = new Dictionary<KEYTYPE, Dictionary<string,Object>>();
	}
	
	
	public void loadAll()
	{
		//Load all type
		foreach(DirKeyPair dkp in dirKeyPairs)
			load(dkp.keyType, dkp.directory);
	}
	
	void load(KEYTYPE keyType, string directory)
	{
		Dictionary<string, Object> objDictionary;
		objDictionary = new Dictionary<string, Object>();
		assetList.Add(keyType, objDictionary);
		
		Object[] objs = Resources.LoadAll(directory);
		foreach(Object obj in objs)
		{
			objDictionary.Add(obj.name, obj);
		}
	}
	
	public T[] getAssets<T>(KEYTYPE keyType, string keyWord) where T: Object
	{
		if(assetList.ContainsKey(keyType))
		{
			Dictionary<string, Object> dictionary = null;
			if( assetList.TryGetValue(keyType, out dictionary))
			{
				List<T> relatedAssets = new List<T>();
				foreach(string key in dictionary.Keys)
				{
					if(key.Contains(keyWord))
					{
						relatedAssets.Add((T)dictionary[key]);
					}
				}
				return relatedAssets.ToArray();
			}
		}
		return null;
	}
	
	public T getAsset<T>(KEYTYPE keyType, string assetName)where T: Object
	{
		if(assetList.ContainsKey(keyType))
		{
			Dictionary<string, Object> dictionary = null;
			if( assetList.TryGetValue(keyType, out dictionary))
			{
				Object asset;
				if(dictionary.TryGetValue(assetName, out asset))
					return (T)asset;
			}
		}
		return null;
	}
	
	public string CurrentCell
	{
		set{currentCell = value;}
	}
		
	
	public CellComponent ChosenCell
	{
		get
		{
			Dictionary<string, Object> cellDictionary = null;
			assetList.TryGetValue(KEYTYPE.CELL, out cellDictionary);
			Texture2D texture = null;
			try
			{
				texture =  (Texture2D)cellDictionary[currentCell];
				return new CellComponent(currentCell,texture);
			}
			catch
			{
				return null;
			}; 
		}
		set{}
	}
	
	public int cellCount()
	{
		Dictionary<string, Object> cellDictionary = null;
		assetList.TryGetValue(KEYTYPE.CELL, out cellDictionary);
		return cellDictionary.Values.Count;
	}
	
	public T[] GetAllAsset<T>(KEYTYPE keyType) where T: Object
	{
		Dictionary<string, Object>assetSubDictionary = null;
		assetList.TryGetValue(keyType, out assetSubDictionary);
		T[] objs = new T[assetSubDictionary.Count];
		int count = 0;
		foreach(Object obj in assetSubDictionary.Values)
		{
			objs[count] = (T)obj;
			count++;
		}
		return objs;
	}
	
	static public LevelAssetManager Instance
	{
		get
		{
			return instance;
		}
	}
}
